<?php
/**
 * CreateUser: zlh
 * CreateTime: 2022/4/15 16:24
 * Remark:
 */

namespace App\Models\Auth;

use App\Consts\BaseConsts\BaseConsts;
use App\Consts\TableConsts\TableConsts;
use App\Models\BaseModel;
use App\Scopes\MediationCompanyAndUpComScope;
use App\Scopes\MediationCompanyIdScope;
use App\Scopes\MediationStaffScope;

class RoleModel extends BaseModel
{
    protected $table = TableConsts::CY_ROLE_TABLE_NAME;

    protected $primaryKey = TableConsts::CY_ROLE_TABLE_PRIMARY_KEY;

    protected $fillable = [
        'role_id',
        'role_name',
        'role_code',
        'company_id'
    ];

    protected static function booted()
    {
         static::addGlobalScope(new MediationCompanyIdScope());
//        static::addGlobalScope(new MediationCompanyAndUpComScope());
    }

    public function getRoleIdsByRoleCode($roleCode){
        return $this
            ->select(['role_id'])
            ->where('role_code',$roleCode)->where('status',1)->first();
    }

    public function getRoleList($query,$pageSize)
    {
        return $query
            ->latest()
            ->Paginate($pageSize);
    }

    public function getRoleSelect(){
        return $this
            ->where('status',BaseConsts::STATUS_NORMAL)
            ->select(
                [
                    'role_id',
                    'role_id as key',
                    'role_name'
                ]
            )->get();
    }

    public function getRoleInfo($roleId,$select=["*"]){
        return $this
            ->select($select)
            ->where('role_id',$roleId)
            ->first();
    }

    public function addRole($roleData){
        return $this->insertGetId($roleData);
    }

    public function addRoleNotAuth($roleData){
        return $this->withoutGlobalScopes()->insertGetId($roleData);
    }

    public function editRole($roleData): bool
    {
        return $this
            ->where('role_id',$roleData['role_id'])
            ->update($roleData);
    }




}

